package com.example.admin.controller.job;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.admin.entity.SysJobLog;
import com.example.admin.service.ISysJobLogService;
import com.example.publice.aspect.annotation.Log;
import com.example.util.APIState;
import com.example.util.AjaxResult;
import com.example.util.BusinessType;
import com.example.util.page.Pager;
import com.example.util.page.SeachParamsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * <p>
 * 定时任务调度日志表 前端控制器
 * </p>
 *
 * @author wangkai
 * @since 2020-09-29
 */
@Api(value = "定时任务调度日志Controller", tags = {"定时任务调度日志操作接口"})
@RestController
@RequestMapping("/sys/sysJobLog")
public class SysJobLogController {

    @Autowired
    private ISysJobLogService logService;

    @ApiOperation(value = "定时任务调度日志列表", httpMethod = "POST")
    @PostMapping(value = "/page")
    public AjaxResult page(@RequestBody Pager pager) {
        Page<SysJobLog> page = new Page<>();
        page.setCurrent(pager.getPageNum());
        page.setSize(pager.getPageSize());
        QueryWrapper<SysJobLog> queryWrapper = new QueryWrapper<>();
        SeachParamsUtil.loadSearchParam(pager, queryWrapper);
        queryWrapper.orderByDesc("create_time");
        pager.convertPage(logService.page(page, queryWrapper));
        return new AjaxResult(APIState.S00, pager);
    }

    @ApiOperation(value = "定时任务日志删除", httpMethod = "DELETE")
    @Log(title = "定时任务日志删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable Integer[] ids) {
        if (logService.removeByIds(Arrays.asList(ids))) {
            return new AjaxResult(APIState.S00, "删除成功");
        }
        return new AjaxResult(APIState.S01, "删除失败");
    }

    @ApiOperation(value = "定时任务日志清空", httpMethod = "DELETE")
    @Log(title = "定时任务日志清空", businessType = BusinessType.DELETE)
    @DeleteMapping("/all")
    public AjaxResult delete() {
        QueryWrapper<SysJobLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("status", 0, 1);
        if (logService.remove(queryWrapper)) {
            return new AjaxResult(APIState.S00, "删除成功");
        }
        return new AjaxResult(APIState.S01, "删除失败");
    }


}
